package com.yhy.tradesystem.techdata;

import java.util.ArrayList;

import com.yhy.tradesystem.TradeDataPak;

public class MAFilter extends BaseTechDataFilter {

	public static final String PARAMETER_MA_DAY = "移动平均线周期";
	public static final String DATA_MA = "移动平均线";
	
	protected int maDay=10;
	protected ArrayList<Float> dataBuffer = new ArrayList<Float>();
	
	private String[] parameterKeySet = {PARAMETER_MA_DAY};
	private float[] parameterValueSet = {maDay};
	private String[] techDataKeySet = {DATA_MA};
	private float[] techDataValueSet = {0.0f};
	
	String filterName = DATA_MA;
	
	
	@Override
	public void inputData(TradeDataPak data) {
		super.inputData(data);
		dataBuffer.add(data.stockData.close);
		outputData();
	}

	@Override
	public void setParameters(String name, float value) {
		if (PARAMETER_MA_DAY.equals(name)) {
			maDay = (int) value;
			filterName  = DATA_MA+"("+maDay+")";
		}
	}

	@Override
	public float getParameters(String name) {
		if (PARAMETER_MA_DAY.equals(name)) {
			return (float)maDay;
		}
		return -1f;
	}

	@Override
	public void resetData() {
		super.resetData();
		dataBuffer.clear();
		filterName = DATA_MA;
	}

	@Override
	public String getName() {
		return DATA_MA;
	}

	@Override
	public TradeDataPak outputData() {
		float result =0.0f;
		if(dataBuffer.size()>maDay){
			dataBuffer.remove(0);
		}
		if(dataBuffer.size()==maDay){
			for(Float data:dataBuffer){
				result+=data;
			}
			result=result/maDay;
		}
		techDataValueSet[0] = result;
		TradeDataPak.TechnicalAnalysisData techdData=new TradeDataPak.TechnicalAnalysisData(getName(), parameterKeySet,parameterValueSet,techDataKeySet,techDataValueSet);
		pak.techDataList.add(techdData);
		return super.outputData();
	}

}
